package com.ruoyi.exitwarehouse.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.enums.RuleType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.fee.domain.WOverdueFeeRule;
import com.ruoyi.fee.domain.WWarehouseFeeRule;
import com.ruoyi.fee.service.IWOverdueFeeRuleService;
import com.ruoyi.fee.service.IWWarehouseFeeRuleService;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.domain.TGoodsWeightSum;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.system.domain.TVehicleDriverInfo;
import com.ruoyi.system.service.ITVehicleDriverInfoService;
import com.ruoyi.warehouse.domain.WWarehouseFeeAccount;
import com.ruoyi.warehouse.service.IWWarehouseFeeAccountService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exitwarehouse.domain.WExitWarehouseInfo;
import com.ruoyi.exitwarehouse.service.IWExitWarehouseInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 出库信息Controller
 * 
 * @author wzy
 * @date 2024-09-01 21:53:21
 */
@RestController
@RequestMapping("/exitwarehouse/exit")
public class WExitWarehouseInfoController extends BaseController
{
    @Autowired
    private IWExitWarehouseInfoService wExitWarehouseInfoService;

    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWWarehouseFeeRuleService wWarehouseFeeRuleService;

    @Autowired
    private IWOverdueFeeRuleService wOverdueFeeRuleService;

    @Autowired
    private IWWarehouseFeeAccountService wWarehouseFeeAccountService;

    @Autowired
    private ITVehicleDriverInfoService tVehicleDriverInfoService;

    private static Logger logger = LoggerFactory.getLogger(WExitWarehouseInfoController.class);
    /**
     * 查询出库信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('exitwarehouse:exit:list, exitwarehouse:pick:list, exitwarehouse:plan:list, exitwarehouse:task:list, exitwarehouse:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(WExitWarehouseInfo wExitWarehouseInfo)
    {
        startPage();
        List<WExitWarehouseInfo> list = wExitWarehouseInfoService.selectWExitWarehouseInfoList(wExitWarehouseInfo);
        return getDataTable(list);
    }

    /**
     * 导出出库信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('exitwarehouse:exit:export, exitwarehouse:pick:export, exitwarehouse:plan:export, exitwarehouse:task:export, exitwarehouse:receipt:export')")
    @Log(title = "出库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WExitWarehouseInfo wExitWarehouseInfo)
    {
        List<WExitWarehouseInfo> list = wExitWarehouseInfoService.selectWExitWarehouseInfoList(wExitWarehouseInfo);
        ExcelUtil<WExitWarehouseInfo> util = new ExcelUtil<WExitWarehouseInfo>(WExitWarehouseInfo.class);
        util.exportExcel(response, list, "出库信息数据");
    }

    /**
     * 获取出库信息详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('exitwarehouse:exit:query, exitwarehouse:pick:query, exitwarehouse:plan:query, exitwarehouse:task:query, exitwarehouse:receipt:query')")
    @GetMapping(value = "/{exitWarehouseId}")
    public AjaxResult getInfo(@PathVariable("exitWarehouseId") Long exitWarehouseId)
    {
        return success(wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseId(exitWarehouseId));
    }

    /**
     * 新增出库信息
     */
    @PreAuthorize("@ss.hasAnyPermi('exitwarehouse:exit:add, exitwarehouse:pick:add, exitwarehouse:plan:add, exitwarehouse:task:add, exitwarehouse:receipt:add')")
    @Log(title = "出库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WExitWarehouseInfo wExitWarehouseInfo)
    {
        wExitWarehouseInfo.setExitWarehouseNumber(OrderUtils.getOrderNumber("EX", "-"));
        wExitWarehouseInfo.setCreateBy(getNickName());
        updateMobileAndNumberRelateInfo(wExitWarehouseInfo);
        return toAjax(wExitWarehouseInfoService.insertWExitWarehouseInfo(wExitWarehouseInfo));
    }

    /**
     * 修改出库信息
     */
    @PreAuthorize("@ss.hasAnyPermi('exitwarehouse:exit:edit, exitwarehouse:pick:edit, exitwarehouse:plan:edit, exitwarehouse:task:edit, exitwarehouse:receipt:edit')")
    @Log(title = "出库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WExitWarehouseInfo wExitWarehouseInfo)
    {
        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WExitWarehouseInfo exitWarehouseInfo = wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseId(wExitWarehouseInfo.getExitWarehouseId());
        if(StringUtils.isNull(exitWarehouseInfo)) {
            return error("当前作业，已删除，作业修改失败！");
        } else if(exitWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认出库，修改失败！");
        }

        wExitWarehouseInfo.setUpdateBy(getNickName());
        return toAjax(wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo));
    }

    /**
     * 删除出库信息
     */
    @PreAuthorize("@ss.hasAnyPermi('exitwarehouse:exit:remove, exitwarehouse:pick:remove, exitwarehouse:plan:remove, exitwarehouse:task:remove, exitwarehouse:receipt:remove')")
    @Log(title = "出库信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{exitWarehouseIds}")
    public AjaxResult remove(@PathVariable Long[] exitWarehouseIds)
    {
        for(Long exitWarehouseId : exitWarehouseIds) {
            WExitWarehouseInfo exitWarehouseInfo = wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseId(exitWarehouseId);
            if(StringUtils.isNotNull(exitWarehouseInfo) && exitWarehouseInfo.getJobStatus().equals("3")) {
                return error("当前作业，已确认出库，删除失败！");
            }
        }

        /* 清除货物信息中，出库相关信息 */
        WExitWarehouseInfo exitWarehouseInfo = new WExitWarehouseInfo();
        tGoodsInfoService.updateTGoodsInfoRelExitWarehouseFieldsByExitWarehouseIds(exitWarehouseIds, exitWarehouseInfo, "已在库");
        return toAjax(wExitWarehouseInfoService.deleteWExitWarehouseInfoByExitWarehouseIds(exitWarehouseIds));
    }

    /**
     * 修改出库信息，创建出库计划
     */
    //@PreAuthorize("@ss.hasPermi('exitwarehouse:exit:edit')")
    @Log(title = "出库信息", businessType = BusinessType.UPDATE)
    @PutMapping("/createplan")
    public AjaxResult createplan(@RequestBody WExitWarehouseInfo wExitWarehouseInfo)
    {
        wExitWarehouseInfo.setUpdateBy(getNickName());
        return toAjax(wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo));
    }

    /**
     * 修改出库单状态，确认出库
     */
    //@PreAuthorize("@ss.hasPermi('exitwarehouse:exit:confirm')")
    @Log(title = "确认出库", businessType = BusinessType.CONFIRM)
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody WExitWarehouseInfo wExitWarehouseInfo)
    {
        Integer rtn = 0;
        //logger.debug("WWW " + "wExitWarehouseInfo = " + wExitWarehouseInfo);
        if (StringUtils.isNull(wExitWarehouseInfo)) {
            return error("出库作业不能为空！");
        }

        // 判断当前作业在数据库中的状态，防止2人重复操作
        WExitWarehouseInfo exitWarehouseInfo = wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseId(wExitWarehouseInfo.getExitWarehouseId());
        if(exitWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认出库，请勿重复操作！");
        }

        List<TGoodsInfo> goodsList = tGoodsInfoService.selectTGoodsInfoByNumber(3, wExitWarehouseInfo.getExitWarehouseNumber());
        if(goodsList.isEmpty()) {
            return error("货物不能为空！");
        }

        wExitWarehouseInfo.setExitWarehouseCustomer(exitWarehouseInfo.getExitWarehouseCustomer());
        wExitWarehouseInfo.setExitWarehouseDriver(exitWarehouseInfo.getExitWarehouseDriver());
        wExitWarehouseInfo.setExitWarehouseVehicle(exitWarehouseInfo.getExitWarehouseVehicle());
        wExitWarehouseInfo.setExitWarehouseTime(exitWarehouseInfo.getExitWarehouseTime());
        wExitWarehouseInfo.setUpdateBy(getNickName());
        wExitWarehouseInfo.setExitWarehouseConfirmPerson(getNickName());
        wExitWarehouseInfo.setExitWarehouseConfirmTime(DateUtils.getNowDate());

        // 出库账单，先计算账单，费用更新中，更新了库存状态，防止出库状态不对，待优化，货物字段更新范围
        rtn = handleWarehouseFeeAccount(wExitWarehouseInfo, goodsList);
        if(rtn < 1) {
            logger.error("确认出库，出库账单生成失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }

        // 更新货物相关信息
        rtn = tGoodsInfoService.updateTGoodsConfirmRelFieldsByExitWarehouseNumber(wExitWarehouseInfo, "已出库");
        if(rtn < 1) {
            logger.error("确认出库，货物信息更新失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }

        // 更新出库单
        wExitWarehouseInfo.setJobStatus("3");
        rtn = wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo);
        if(rtn < 1) {
            logger.error("确认出库，出库作业更新失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }

        return toAjax(rtn);
    }

    /**
     * 修改出库单状态，撤销出库
     */
    //@PreAuthorize("@ss.hasPermi('exitwarehouse:exit:revoke')")
    @Log(title = "撤销出库", businessType = BusinessType.REVOKE)
    @PutMapping("/revoke")
    public AjaxResult revoke(@RequestBody WExitWarehouseInfo wExitWarehouseInfo)
    {
        Integer rtn = 0;
        //logger.debug("WWW " + "wExitWarehouseInfo = " + wExitWarehouseInfo);
        if (StringUtils.isNull(wExitWarehouseInfo)) {
            return error("出库单据不能为空！");
        }

        // 判断当前作业在数据库中的状态，防止2人重复操作
        WExitWarehouseInfo exitWarehouseInfo = wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseId(wExitWarehouseInfo.getExitWarehouseId());
        if(exitWarehouseInfo.getJobStatus().equals("2")) {
            return error("当前单据，已撤销出库，请勿重复操作！");
        }

        //撤销出库判断确认出库时间，出库时间超过2天，不允许撤销
        LocalDate today = LocalDate.now();
        //logger.debug("当前日期: " + today);
        LocalDate exitWarehouseConfirmTime = exitWarehouseInfo.getExitWarehouseConfirmTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //logger.debug("确认时间: " + exitWarehouseConfirmTime);
        long daysBetween = ChronoUnit.DAYS.between(exitWarehouseConfirmTime, today);
        if(daysBetween > 4) {
            //logger.debug("时间相差: " + daysBetween);
            //return error("确认出库时间，据当前时间，已经超过4天，不允许撤销");
        }

        wExitWarehouseInfo.setUpdateBy(getNickName());
        wExitWarehouseInfo.setExitWarehouseConfirmPerson(null);
        wExitWarehouseInfo.setExitWarehouseConfirmTime(null);

        // 更新货物相关信息
        rtn = tGoodsInfoService.updateTGoodsConfirmRelFieldsByExitWarehouseNumber(wExitWarehouseInfo, "出库中");
        if(rtn < 1) {
            logger.error("撤销出库，货物信息更新失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }

        // 删除账单
        rtn = wWarehouseFeeAccountService.deleteWWarehouseFeeAccountByWarehouseFeeAccountNumber(wExitWarehouseInfo.getExitWarehouseNumber().replace("EX","WA"));
        if(rtn < 1) {
            logger.error("撤销出库，出库账单删除失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }

        // 更新出库单
        wExitWarehouseInfo.setJobStatus("2");
        rtn = wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo);
        if(rtn < 1) {
            logger.error("撤销出库，出库单据更新失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }

        return toAjax(rtn);
    }

    /**
     *处理仓储费账单
     */
    private Integer handleWarehouseFeeAccount(WExitWarehouseInfo wExitWarehouseInfo, List<TGoodsInfo> goodsList)
    {
        Integer rtn = 0;

        //List<TGoodsInfo> goodsList = tGoodsInfoService.selectTGoodsInfoByNumber(3, wExitWarehouseInfo.getExitWarehouseNumber());
        // 货物列表为空，返回0
        if(goodsList.isEmpty()) return 0;
        String goodsType = wExitWarehouseInfo.getGoodsType();
        Double indoorWarehousePrice = 0.0;
        Double outdoorWarehousePrice = 0.0;
        Double warehouseWeight = 0.0;
        Double warehouseFee = 0.0;
        Double goodsWarehouseFee = 0.0;
        Double goodsWeight = 0.0;

        WWarehouseFeeAccount warehouseFeeAccount = new WWarehouseFeeAccount();
        warehouseFeeAccount.setWarehouseFeeAccountNumber(wExitWarehouseInfo.getExitWarehouseNumber().replace("EX","WA"));
        //仓储费列表
        List<WWarehouseFeeRule> warehouseFeelist = wWarehouseFeeRuleService.selectWWarehouseFeeRuleListByCustomerName(wExitWarehouseInfo.getExitWarehouseCustomer());
        //logger.debug("WWW " + "warehouseFeelist = " + warehouseFeelist);
        if(warehouseFeelist.size() > 0) {
            for (WWarehouseFeeRule item : warehouseFeelist) {
                if (item.getRuleType().equals(RuleType.COMMON.getInfo()) && item.getGoodsType().equals(goodsType)) {
                    indoorWarehousePrice = item.getIndoorWarehousePrice();
                    outdoorWarehousePrice = item.getOutdoorWarehousePrice();
                }
                if (item.getRuleType().equals(RuleType.CUSTOMIZED.getInfo()) && item.getGoodsType().equals(goodsType)) {
                    indoorWarehousePrice = item.getIndoorWarehousePrice();
                    outdoorWarehousePrice = item.getOutdoorWarehousePrice();
                }
            }

            for (TGoodsInfo goods : goodsList) {
                goodsWeight = goods.getWeight();
                warehouseWeight = warehouseWeight + goodsWeight;
                goods.setIndoorWarehousePrice(indoorWarehousePrice);
                goods.setOutdoorWarehousePrice(outdoorWarehousePrice);
//                logger.debug("handleWarehouseFeeAccount ... item.getWarehouseType() = {}", item.getWarehouseType());
                if ((!(goods.getWarehouseType() == null || goods.getWarehouseType().isEmpty())) && goods.getWarehouseType().equals("室外库")) {
                    goodsWarehouseFee = Math.round(outdoorWarehousePrice * goodsWeight * 100.0) / 100.0;
                } else {
                    goodsWarehouseFee = Math.round(indoorWarehousePrice * goodsWeight * 100.0) / 100.0;
                }
                goods.setStorageFee(goodsWarehouseFee);
                warehouseFee = warehouseFee + goodsWarehouseFee;
            }
            warehouseWeight = Math.round(warehouseWeight * 1000.0) / 1000.0;
            warehouseFeeAccount.setWarehouseWeight(warehouseWeight);
            warehouseFeeAccount.setIndoorWarehousePrice(indoorWarehousePrice);
            warehouseFeeAccount.setOutdoorWarehousePrice(outdoorWarehousePrice);
            warehouseFee = Math.round(warehouseFee * 100.0) / 100.0;
            warehouseFeeAccount.setStorageFee(warehouseFee);
        } else {
            logger.error("No Warehouse Fee Rule ...");
        }

        Integer freeStorageDays = 0;
        String overduePriceMode = "";
        Double overduePrice = 0.0;
        Double overdueGoodsFee = 0.0;
        Double overdueWarehouseFee = 0.0;
        Integer betweenDays = 0, storageDays = 0;

        // 超期费列表
        List<WOverdueFeeRule> overdueFeelist = wOverdueFeeRuleService.selectWOverdueFeeRuleListByCustomerName(wExitWarehouseInfo.getExitWarehouseCustomer());
//        logger.debug("WWW " + "overdueFeelist = " + overdueFeelist);
        if(overdueFeelist.size() > 0) {
            for (WOverdueFeeRule item : overdueFeelist) {
                if (item.getRuleType().equals(RuleType.COMMON.getInfo()) && item.getGoodsType().equals(goodsType)) {
                    freeStorageDays = item.getFreeStorage();
                    overduePriceMode = item.getOverduePriceMode();
                    overduePrice = item.getOverduePrice();
                }
                if (item.getRuleType().equals(RuleType.CUSTOMIZED.getInfo()) && item.getGoodsType().equals(goodsType)) {
                    freeStorageDays = item.getFreeStorage();
                    overduePriceMode = item.getOverduePriceMode();
                    overduePrice = item.getOverduePrice();
                }
            }
            //从入库单据，货物，入手
            for (TGoodsInfo goods : goodsList) {
                goods.setFreeStorage(freeStorageDays);
                goods.setOverduePriceMode(overduePriceMode);
                goods.setOverduePrice(overduePrice);
                if( (goods.getExitWarehouseTime() == null) || (goods.getEnterWarehouseTime() == null)) {
                    betweenDays = 0;
                    logger.error("ExitWarehouseTime or EnterWarehouseTime is null, betweenDays = 0");
                } else {
                    betweenDays = Math.toIntExact((goods.getExitWarehouseTime().getTime() - goods.getEnterWarehouseTime().getTime())/(24 * 60 * 60 *1000));
                }
//            logger.debug("WWW " + "betweenDays  = " + betweenDays);
//            logger.debug("WWW " + "freeStorageDays  = " + freeStorageDays);
                if (overduePriceMode.equals("吨x天")) {
                    if (betweenDays > freeStorageDays) {
                        overdueGoodsFee = overduePrice * (betweenDays - freeStorageDays) * goods.getWeight();
                        overdueGoodsFee = Math.round(overdueGoodsFee * 100.0) / 100.0;
                        goods.setOverdueFee(overdueGoodsFee);
                        overdueWarehouseFee = overdueWarehouseFee + overdueGoodsFee;
                    } else {
                        goods.setOverdueFee(0.0);
                    }
                } else if (overduePriceMode.equals("吨x月")) {
                    // 每月按30天计算
                    if ((betweenDays - freeStorageDays) < 30) {
                        goods.setOverdueFee(0.0);
                    } else {
                        overdueGoodsFee = overduePrice * (betweenDays - freeStorageDays) * goods.getWeight() / 30.0;
                        overdueGoodsFee = Math.round(overdueGoodsFee * 100.0) / 100.0;
                        goods.setOverdueFee(overdueGoodsFee);
                        overdueWarehouseFee = overdueWarehouseFee + overdueGoodsFee;
                    }
                }
                goods.setStorageDay(betweenDays);
//                tGoodsInfoService.updateTGoodsInfo(item);
                storageDays = storageDays + betweenDays;
            }
            //超期费，免堆天数
            warehouseFeeAccount.setFreeStorage(freeStorageDays);
            //超期费，计算模式
            warehouseFeeAccount.setOverduePriceMode(overduePriceMode);
            //超期费，库存天数
            warehouseFeeAccount.setStorageDay(storageDays);
            //超期费，单价
            warehouseFeeAccount.setOverduePrice(overduePrice);
            //超期费，总数
            warehouseFeeAccount.setOverdueFee(overdueGoodsFee);
            //账单，结算模式
            warehouseFeeAccount.setWarehouseSettlementMode(wExitWarehouseInfo.getSettlementMode());
            warehouseFeeAccount.setCreateBy(getNickName());
            warehouseFeeAccount.setCreateTime(DateUtils.getNowDate());
            //仓储费合计
            warehouseFeeAccount.setWarehouseFeeTotal(Math.round(((warehouseFee + overdueWarehouseFee) * 100.0)) / 100.0);
//        warehouseFeeAccount.setWarehouseFeeTotal(warehouseFee + overdueWarehouseFee);

            warehouseFeeAccount.setWarehouseSettlementCustomer(wExitWarehouseInfo.getExitWarehouseCustomer());
            warehouseFeeAccount.setWarehouseSettlementMode(wExitWarehouseInfo.getSettlementMode());
            warehouseFeeAccount.setWarehouseSettlementMode(wExitWarehouseInfo.getSettlementMode());
        } else {
            logger.error("No Overdue Fee Rule ...");
        }
        // 批量更新货物仓储费相关字段，货物列表不为空
        rtn = tGoodsInfoService.batchUpdateTGoodsWarehouseFeeRelFields(goodsList);
        if(rtn < 1) {
            logger.error("确认出库，批量更新货物仓储费相关字段失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }
        // 删除账单
        rtn = wWarehouseFeeAccountService.deleteWWarehouseFeeAccountByWarehouseFeeAccountNumber(warehouseFeeAccount.getWarehouseFeeAccountNumber());
        if(rtn < 1) {
            logger.error("确认出库，删除账单失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }
        // 插入账单
        rtn = wWarehouseFeeAccountService.insertWWarehouseFeeAccount(warehouseFeeAccount);
        if(rtn < 1) {
            logger.error("确认出库，插入账单失败。出库单号：" + wExitWarehouseInfo.getExitWarehouseNumber());
        }
        // 更新出库信息
        //rtn = wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo);

        return rtn;
    }

    /**
     * 获取货物重量合计
     */
    @GetMapping(value = "/exitweightsum")
    public AjaxResult exitweightsum(WExitWarehouseInfo wExitWarehouseInfo)
    {
        List<TGoodsWeightSum> list = wExitWarehouseInfoService.getWeightSumByExitWarehouseInfo(wExitWarehouseInfo);
//        logger.debug("GGG " + "getWeightSumByExitWarehouseInfo = " + list);
        return success(list);
    }

    /**
     * 更新“车牌号”相关信息
     */
    private void updateMobileAndNumberRelateInfo(WExitWarehouseInfo wExitWarehouseInfo) {
        TVehicleDriverInfo tVehicleDriverInfo = new TVehicleDriverInfo();
        //更新车牌，司机，电话
        if(!(wExitWarehouseInfo.getExitWarehouseVehicle() == null || wExitWarehouseInfo.getExitWarehouseVehicle().isEmpty())) {
//            logger.debug("HHH " + "edit = " + tVehicleDriverInfo);
            tVehicleDriverInfo.setVehicleLicenseNumber(wExitWarehouseInfo.getExitWarehouseVehicle());
            List<TVehicleDriverInfo> list = tVehicleDriverInfoService.selectTVehicleDriverInfoByNumber(tVehicleDriverInfo);
            tVehicleDriverInfo.setDriverName(wExitWarehouseInfo.getExitWarehouseDriver());
            tVehicleDriverInfo.setContactMobile(wExitWarehouseInfo.getExitWarehouseMobile());
            if (list.isEmpty()) {
                if((tVehicleDriverInfo.getDriverName() == null || tVehicleDriverInfo.getDriverName().isEmpty())
                        && (tVehicleDriverInfo.getContactMobile() == null || tVehicleDriverInfo.getContactMobile().isEmpty())) {return;}
                tVehicleDriverInfo.setCreateBy(getNickName());
                tVehicleDriverInfo.setUpdateBy(null);
                tVehicleDriverInfoService.insertTVehicleDriverInfo(tVehicleDriverInfo);
            } else {
                tVehicleDriverInfo.setVehicleDriverId(list.get(0).getVehicleDriverId());
                tVehicleDriverInfo.setUpdateBy(getNickName());
                tVehicleDriverInfoService.updateTVehicleDriverInfo(tVehicleDriverInfo);
            }
        } else {
            logger.debug("Vehicle HHH Number is empty ");
        }
//
    }

    // 出库加货，出库移除，从货物信息中，移动到此处
    /**
     * 出库加货，更新货物信息，中出库相关字段
     */
    //@PreAuthorize("@ss.hasPermi('goods:good:edit')")
    @Log(title = "货物信息", businessType = BusinessType.UPDATE)
    @PutMapping("/exitnumber/{goodsIds}/{exitWarehouseNumber}")
    public AjaxResult updateExitWarehouseNumber(@PathVariable Long[] goodsIds, @PathVariable String exitWarehouseNumber, @RequestBody WExitWarehouseInfo wExitWarehouseInfo)
    {
        //logger.debug("XXX exitnumber add goods exitWarehouseInfo" + exitWarehouseInfo);
        WExitWarehouseInfo exitWarehouseInfo = wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseNumber(exitWarehouseNumber);
        if(StringUtils.isNull(exitWarehouseInfo)) {
            return error("当前作业，已删除，货物加货失败！");
        } else if(exitWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认出库，加货失败！");
        }
        int rtn = tGoodsInfoService.updateTGoodsInfoRelExitWarehouseFieldsByGoodsIds(goodsIds, exitWarehouseNumber, wExitWarehouseInfo, "出库中");

        if((rtn > 0) && StringUtils.isNotNull(exitWarehouseNumber))
        {
            rtn = wExitWarehouseInfoService.updateWExitTotalWeightPieceByNumber(exitWarehouseNumber);
            if(rtn == 0)
            {
                logger.error("exit warehouse Add goods " + "update Exit Total Weight Piece error, exitWarehouseNumber " + exitWarehouseNumber);
            }
        } else {
            logger.error("exit warehouse Add goods " + "updateTGoodsInfoRelExitWarehouseFieldsByGoodsIds error, exitWarehouseNumber " + exitWarehouseNumber + ", goodsIds, " + Arrays.toString(goodsIds));
        }

        return toAjax(rtn);
    }

    /**
     * 出库移除，清空货物信息，中出库相关字段
     */
    //@PreAuthorize("@ss.hasPermi('goods:good:remove')")
    @Log(title = "货物信息", businessType = BusinessType.REMOVE)
    @DeleteMapping("/exitnumber/{goodsIds}/{exitWarehouseNumber}")
    public AjaxResult removeExitWarehouseNumber(@PathVariable Long[] goodsIds, @PathVariable String exitWarehouseNumber)
    {
        String operator = getNickName();
        // 货物移除打印此信息，重点监控，异常移除问题
        logger.info("YYY removeExitWarehouseNumber " + "goodsIds = " + Arrays.toString(goodsIds) + " exitWarehouseNumber = " + exitWarehouseNumber + " operator = " + operator);
        WExitWarehouseInfo exitWarehouseInfo = wExitWarehouseInfoService.selectWExitWarehouseInfoByExitWarehouseNumber(exitWarehouseNumber);
        if(StringUtils.isNull(exitWarehouseInfo)) {
            return error("当前作业，已删除，货物移除失败！");
        } else if(exitWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认出库，移除失败！");
        }
        int rtn = tGoodsInfoService.removeTGoodsExitWarehouseRelFieldByGoodsIds(goodsIds, exitWarehouseNumber, operator);
        if((rtn > 0) && StringUtils.isNotNull(exitWarehouseNumber))
        {
            rtn = wExitWarehouseInfoService.updateWExitTotalWeightPieceByNumber(exitWarehouseNumber);
            if(rtn == 0)
            {
                logger.error("exit warehouse Remove goods " + "update Exit Total Weight Piece error, exitWarehouseNumber " + exitWarehouseNumber);
            }
        } else {
            logger.error("exit warehouse Remove goods " + "removeTGoodsExitWarehouseRelFieldByGoodsIds error, exitWarehouseNumber " + exitWarehouseNumber + ", goodsIds, " + Arrays.toString(goodsIds));
        }

        return toAjax(rtn);
    }

    /**
     * 获取出库重量模式
     */
    @GetMapping(value = "/weightmode/{contractNumber}")
    public AjaxResult getWeightMode(@PathVariable("contractNumber") String contractNumber)
    {
        //String weightMode = tGoodsInfoService.selectTGoodsWeightModeByContractNumber(contractNumber);
        return success(tGoodsInfoService.selectTGoodsWeightModeByContractNumber(contractNumber));
    }

}
